function results = key_technologies_overview(varargin)
%% 关键技术概述函数
% 展示无线通信系统的关键技术，包括调制技术和多址技术
%
% 输入参数（可选）：
%   'show_plots' - 是否显示图形 (默认: true)
%   'save_results' - 是否保存结果 (默认: false)
%
% 输出：
%   results - 包含技术数据的结构体
%
% 作者：周勇
% 日期：2024年

%% 参数解析
p = inputParser;
addParameter(p, 'show_plots', true, @islogical);
addParameter(p, 'save_results', false, @islogical);
parse(p, varargin{:});

show_plots = p.Results.show_plots;
save_results = p.Results.save_results;

%% 调制技术数据
modulation_types = {'BPSK', 'QPSK', '16QAM', '64QAM', '256QAM', '1024QAM'};
bits_per_symbol = [1, 2, 4, 6, 8, 10];
spectral_efficiency = bits_per_symbol; % bps/Hz

%% 多址技术数据
multiple_access = {'FDMA', 'TDMA', 'CDMA', 'OFDMA'};
efficiency = [0.5, 0.7, 0.8, 0.9];
complexity = [1, 2, 4, 6];

%% 创建结果结构体
results.modulation_types = modulation_types;
results.bits_per_symbol = bits_per_symbol;
results.spectral_efficiency = spectral_efficiency;
results.multiple_access = multiple_access;
results.efficiency = efficiency;
results.complexity = complexity;

%% 调制技术演进图
if show_plots
    fig_modulation = figure('Name', '调制技术演进', 'Position', [150, 150, 800, 600]);
    stem(bits_per_symbol, spectral_efficiency, 'filled');
    grid on;
    xlabel('每符号比特数');
    ylabel('频谱效率 (bps/Hz)');
    title('调制技术演进与频谱效率');
    xticks(bits_per_symbol);
    xticklabels(modulation_types);
    
    % 添加数值标签
    for i = 1:length(bits_per_symbol)
        text(bits_per_symbol(i), spectral_efficiency(i)+0.2, ...
             sprintf('%.1f', spectral_efficiency(i)), ...
             'HorizontalAlignment', 'center', 'FontSize', 10);
    end
end

%% 多址技术对比图
if show_plots
    fig_multiple_access = figure('Name', '多址技术对比', 'Position', [200, 200, 1000, 700]);
    
    % 频谱效率对比
    subplot(1,2,1);
    bar(efficiency);
    title('频谱效率对比');
    xticklabels(multiple_access);
    ylabel('相对效率');
    ylim([0, 1.2]);
    grid on;
    
    % 添加数值标签
    for i = 1:length(efficiency)
        text(i, efficiency(i)+0.02, sprintf('%.1f', efficiency(i)), ...
             'HorizontalAlignment', 'center', 'FontSize', 10);
    end
    
    % 实现复杂度对比
    subplot(1,2,2);
    bar(complexity);
    title('实现复杂度对比');
    xticklabels(multiple_access);
    ylabel('相对复杂度');
    ylim([0, 7]);
    grid on;
    
    % 添加数值标签
    for i = 1:length(complexity)
        text(i, complexity(i)+0.1, sprintf('%d', complexity(i)), ...
             'HorizontalAlignment', 'center', 'FontSize', 10);
    end
end

%% 无线通信系统架构图
if show_plots
    fig_architecture = figure('Name', '无线通信系统架构', 'Position', [250, 250, 1400, 600]);
    axis off;
    
    % 定义模块位置
    modules = struct(...
        'Source', [0.1, 0.7, 0.15, 0.15], ...
        'SourceEncoder', [0.3, 0.7, 0.15, 0.15], ...
        'ChannelEncoder', [0.5, 0.7, 0.15, 0.15], ...
        'Modulator', [0.7, 0.7, 0.15, 0.15], ...
        'WirelessChannel', [0.1, 0.3, 0.8, 0.15], ...
        'Demodulator', [0.1, 0.1, 0.15, 0.15], ...
        'ChannelDecoder', [0.3, 0.1, 0.15, 0.15], ...
        'SourceDecoder', [0.5, 0.1, 0.15, 0.15], ...
        'Destination', [0.7, 0.1, 0.15, 0.15]);
    
    % 绘制发送端模块
    rectangle('Position', modules.Source, 'FaceColor', [0.8, 0.9, 1], 'EdgeColor', 'b');
    text(modules.Source(1)+modules.Source(3)/2, modules.Source(2)+modules.Source(4)/2, '信源', ...
        'HorizontalAlignment', 'center', 'FontSize', 12, 'FontWeight', 'bold');
    
    rectangle('Position', modules.SourceEncoder, 'FaceColor', [0.8, 0.9, 1], 'EdgeColor', 'b');
    text(modules.SourceEncoder(1)+modules.SourceEncoder(3)/2, modules.SourceEncoder(2)+modules.SourceEncoder(4)/2, '信源编码', ...
        'HorizontalAlignment', 'center', 'FontSize', 12, 'FontWeight', 'bold');
    
    rectangle('Position', modules.ChannelEncoder, 'FaceColor', [0.8, 0.9, 1], 'EdgeColor', 'b');
    text(modules.ChannelEncoder(1)+modules.ChannelEncoder(3)/2, modules.ChannelEncoder(2)+modules.ChannelEncoder(4)/2, '信道编码', ...
        'HorizontalAlignment', 'center', 'FontSize', 12, 'FontWeight', 'bold');
    
    rectangle('Position', modules.Modulator, 'FaceColor', [0.8, 0.9, 1], 'EdgeColor', 'b');
    text(modules.Modulator(1)+modules.Modulator(3)/2, modules.Modulator(2)+modules.Modulator(4)/2, '调制器', ...
        'HorizontalAlignment', 'center', 'FontSize', 12, 'FontWeight', 'bold');
    
    % 绘制无线信道
    rectangle('Position', modules.WirelessChannel, 'FaceColor', [1, 0.9, 0.8], 'EdgeColor', 'r');
    text(modules.WirelessChannel(1)+modules.WirelessChannel(3)/2, modules.WirelessChannel(2)+modules.WirelessChannel(4)/2, '无线信道', ...
        'HorizontalAlignment', 'center', 'FontSize', 14, 'FontWeight', 'bold', 'Color', 'r');
    
    % 绘制接收端模块
    rectangle('Position', modules.Demodulator, 'FaceColor', [0.9, 1, 0.8], 'EdgeColor', 'g');
    text(modules.Demodulator(1)+modules.Demodulator(3)/2, modules.Demodulator(2)+modules.Demodulator(4)/2, '解调器', ...
        'HorizontalAlignment', 'center', 'FontSize', 12, 'FontWeight', 'bold');
    
    rectangle('Position', modules.ChannelDecoder, 'FaceColor', [0.9, 1, 0.8], 'EdgeColor', 'g');
    text(modules.ChannelDecoder(1)+modules.ChannelDecoder(3)/2, modules.ChannelDecoder(2)+modules.ChannelDecoder(4)/2, '信道译码', ...
        'HorizontalAlignment', 'center', 'FontSize', 12, 'FontWeight', 'bold');
    
    rectangle('Position', modules.SourceDecoder, 'FaceColor', [0.9, 1, 0.8], 'EdgeColor', 'g');
    text(modules.SourceDecoder(1)+modules.SourceDecoder(3)/2, modules.SourceDecoder(2)+modules.SourceDecoder(4)/2, '信源译码', ...
        'HorizontalAlignment', 'center', 'FontSize', 12, 'FontWeight', 'bold');
    
    rectangle('Position', modules.Destination, 'FaceColor', [0.9, 1, 0.8], 'EdgeColor', 'g');
    text(modules.Destination(1)+modules.Destination(3)/2, modules.Destination(2)+modules.Destination(4)/2, '信宿', ...
        'HorizontalAlignment', 'center', 'FontSize', 12, 'FontWeight', 'bold');
    
    % 绘制连接线
    % 发送端连接线
    plot([modules.Source(1)+modules.Source(3), modules.SourceEncoder(1)], ...
         [modules.Source(2)+modules.Source(4)/2, modules.SourceEncoder(2)+modules.SourceEncoder(4)/2], 'b-', 'LineWidth', 2);
    plot([modules.SourceEncoder(1)+modules.SourceEncoder(3), modules.ChannelEncoder(1)], ...
         [modules.SourceEncoder(2)+modules.SourceEncoder(4)/2, modules.ChannelEncoder(2)+modules.ChannelEncoder(4)/2], 'b-', 'LineWidth', 2);
    plot([modules.ChannelEncoder(1)+modules.ChannelEncoder(3), modules.Modulator(1)], ...
         [modules.ChannelEncoder(2)+modules.ChannelEncoder(4)/2, modules.Modulator(2)+modules.Modulator(4)/2], 'b-', 'LineWidth', 2);
    
    % 信道到接收端连接线
    plot([modules.WirelessChannel(1), modules.Demodulator(1)+modules.Demodulator(3)/2], ...
         [modules.WirelessChannel(2), modules.Demodulator(2)+modules.Demodulator(4)], 'g-', 'LineWidth', 2);
    plot([modules.WirelessChannel(1)+0.2, modules.Demodulator(1)+modules.Demodulator(3)/2], ...
         [modules.WirelessChannel(2), modules.Demodulator(2)+modules.Demodulator(4)], 'g-', 'LineWidth', 2);
    plot([modules.WirelessChannel(1)+0.4, modules.Demodulator(1)+modules.Demodulator(3)/2], ...
         [modules.WirelessChannel(2), modules.Demodulator(2)+modules.Demodulator(4)], 'g-', 'LineWidth', 2);
    
    % 接收端内部连接线
    plot([modules.Demodulator(1)+modules.Demodulator(3), modules.ChannelDecoder(1)], ...
         [modules.Demodulator(2)+modules.Demodulator(4)/2, modules.ChannelDecoder(2)+modules.ChannelDecoder(4)/2], 'g-', 'LineWidth', 2);
    plot([modules.ChannelDecoder(1)+modules.ChannelDecoder(3), modules.SourceDecoder(1)], ...
         [modules.ChannelDecoder(2)+modules.ChannelDecoder(4)/2, modules.SourceDecoder(2)+modules.SourceDecoder(4)/2], 'g-', 'LineWidth', 2);
    plot([modules.SourceDecoder(1)+modules.SourceDecoder(3), modules.Destination(1)], ...
         [modules.SourceDecoder(2)+modules.SourceDecoder(4)/2, modules.Destination(2)+modules.Destination(4)/2], 'g-', 'LineWidth', 2);
    
    % 添加标题和说明
    title('无线通信系统基本架构', 'FontSize', 16, 'FontWeight', 'bold');
    text(0.5, 0.95, '发送端', 'HorizontalAlignment', 'center', 'FontSize', 14, 'FontWeight', 'bold', 'Color', 'b');
    text(0.5, 0.05, '接收端', 'HorizontalAlignment', 'center', 'FontSize', 14, 'FontWeight', 'bold', 'Color', 'g');
end

%% 保存结果
if save_results
    % 创建结果文件夹（在当前章节目录下）
    if ~exist('results', 'dir')
        mkdir('results');
    end
    
    % 保存数据文件
    save('results/key_technologies_overview.mat', 'results');
    
    % 保存图片文件（如果显示了图形）
    if show_plots
        try
            saveas(fig_modulation, 'results/modulation_technology_evolution.png');
            saveas(fig_multiple_access, 'results/multiple_access_comparison.png');
            saveas(fig_architecture, 'results/wireless_system_architecture.png');
        catch
            % 如果saveas失败（可能是因为UI组件），使用exportapp
            exportapp(fig_modulation, 'results/modulation_technology_evolution.png');
            exportapp(fig_multiple_access, 'results/multiple_access_comparison.png');
            exportapp(fig_architecture, 'results/wireless_system_architecture.png');
        end
        
        % 关闭图形窗口
        close(fig_modulation);
        close(fig_multiple_access);
        close(fig_architecture);
    end
    
    fprintf('结果已保存到 Chapter01_Introduction/results 文件夹中\n');
end

end